package com.luck.pf.security.user.logic;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;

import com.luck.org.entity.SysLog;
import com.luck.org.entity.SysMenu;
import com.luck.org.entity.SysUser;
import com.luck.org.entity.VDepartment;
import com.luck.org.entity.VUser;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.security.menu.logic.IMenuLogic;
import com.luck.pf.security.user.qry.UserQry;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.Page;

/**
 * 说明:
 * Copyright: Copyright (c) 2010
 * @author zhangwr 2011-8-10 下午02:40:13
 * @version 1.0
 */
public class UserLogic extends BaseLogic implements IUserLogic {
	private IMenuLogic menuLogic;
	public Page findUserPage(UserQry qry) throws Exception{
		Criteria criteria = super.baseDao.createCriteria(SysUser.class);
		CriteriaUtil.addFullLike(criteria, "loginname", qry.getLoginname());
		CriteriaUtil.addFullLike(criteria, "realname", qry.getRealname());
		CriteriaUtil.addEq(criteria, "deptId", qry.getDeptId());
		if(StringUtils.isNotBlank(qry.getUserLevel()))
		CriteriaUtil.addEq(criteria, "userLevel", qry.getUserLevel());
		//criteria.add(Restrictions.eq("enabled", qry.getEnabled()));
		criteria.createAlias("dept", "dept");
		filterData(criteria, qry.getUserInfo(), "dept", null);
		return super.baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}
	public boolean checkUser(SysUser user) {
		Criteria criteria = baseDao.createCriteria(SysUser.class);
		CriteriaUtil.addEq(criteria, "loginname", user.getLoginname());
		List<SysUser> ls = criteria.list();
		if(ls != null && !ls.isEmpty()){
			if(user.getId()==null || !user.getId().equals(ls.get(0).getId()))
				return false;
		}
		return true;
	}
	public SysUser findUserByLoginname(String loginname){
		List<SysUser> list = super.baseDao.find(" FROM SysUser WHERE loginname=?",loginname);
		if(null != list && !list.isEmpty())
			return list.get(0);
		return null;
	}

	public UserBean findUserBean(String loginname) {
		UserBean userBean = new UserBean();
		SysUser user = findUserByLoginname(loginname);
		//机构
		VDepartment org = null;
		//部门
		VDepartment dept = super.baseDao.get(VDepartment.class, user.getDept().getId());
		if(dept.getDeptKind().equals("1"))//机构
			org = dept;
		else{
			org= super.baseDao.get(VDepartment.class, dept.getParentId());
		}
		userBean.setDeptId(user.getDept().getId());
		userBean.setLoginName(user.getLoginname());
		userBean.setUserId(user.getId());
		userBean.setDept(dept);
		userBean.setOrg(org);
		userBean.setOrgId(org.getId());
		userBean.setUser(super.baseDao.get(VUser.class, user.getId()));
		List<SysMenu> menus = menuLogic.findUserMenu(loginname, "ROOT_RES","ROOT_RES");
		List resources = new ArrayList();
		for(SysMenu menu:menus){
			if(StringUtils.isNotBlank(menu.getResItemId()))
				resources.add(menu.getResItemId());
		}
		userBean.setResources(resources);
		return userBean;
	}

	public String saveUser(SysUser user) throws Exception{
		StringBuffer bf = new StringBuffer();
		super.baseDao.save(user);
		return bf.toString();
	}
	public String deleteUser(SysUser user,UserBean userBean, SysLog log) throws Exception{
		StringBuffer bf = new StringBuffer();
		if(!userBean.getUser().getUserLevel().equals("1"))
			bf.append("您不是管理员<br/>");
		if(userBean.getLoginName().equals(user.getLoginname()))
			bf.append("此用户是自己<br/>");
		//处理此用户是否有数据
		if(StringUtils.isBlank(bf.toString())){
			//super.baseDao.delete(user);
			user.setEnabled(0);
			save(user);
		}else
			throw new Exception(bf.toString());
		return bf.toString();
	}
	public String saveUserStatus(UserBean userBean, SysUser entity) throws Exception{
		StringBuffer bf = new StringBuffer();
		if(entity.getId() == 1L)
			bf.append("此为超级管理员<br/>");
		if(userBean.getUserId().equals(entity.getId()))
			bf.append("此为您自己<br/>");
		if(new Long(userBean.getDept().getDeptLevel())> new Long(entity.getDept().getDeptLevel()))
			bf.append("此用户机构级别必须低于您所属机构<br/>");
		if(!entity.getDept().getTreeNo().startsWith(userBean.getDept().getTreeNo()))
			bf.append("此用户机构不是您下属机构<br/>");
		if(StringUtils.isBlank(bf.toString())){
			if(entity.getEnabled()==1)
				entity.setEnabled(0);
			else
				entity.setEnabled(1);
			if(StringUtils.isBlank(entity.getSystemFlag()))
				entity.setSystemFlag("ROOT");
			super.baseDao.save(entity);
		}
		return bf.toString();
	}
	public List<SysUser> findUserByDeptId(Long deptId){
		return super.baseDao.find(" FROM SysUser WHERE deptId=?",deptId);
	}
	public IMenuLogic getMenuLogic() {
		return menuLogic;
	}

	public void setMenuLogic(IMenuLogic menuLogic) {
		this.menuLogic = menuLogic;
	}
}
